package com.vicene.gmail.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.vicene.gmail.common.exception.CommonCodeEnum;
import com.vicene.gmail.member.exception.PhoneExistException;
import com.vicene.gmail.member.exception.UsernameExistException;
import com.vicene.gmail.member.feign.CouponFeignService;
import com.vicene.gmail.member.vo.MemberLoginVo;
import com.vicene.gmail.member.vo.MemberRegistVo;
import com.vicene.gmail.member.vo.WeiBoAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.vicene.gmail.member.entity.MemberEntity;
import com.vicene.gmail.member.service.MemberService;
import com.vicene.gmail.common.utils.PageUtils;
import com.vicene.gmail.common.utils.R;



/**
 * 会员
 *
 * @author vicene
 * @email 505543479@qq.com
 * @date 2020-10-16 15:09:12
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;


    @Autowired
    CouponFeignService couponFeignService;

    @RequestMapping("/coupons")
    public R test(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");

        R membercoupons = couponFeignService.membercoupons();
        return R.ok().put("member" ,memberEntity ).put("coupons" , membercoupons.get("coupons"));
    }

    @RequestMapping("/regist")
    public R regist(@RequestBody MemberRegistVo vo){
        try {
            memberService.regist(vo);
        } catch (PhoneExistException e) {
            e.printStackTrace();
            return R.error(CommonCodeEnum.PHONE_EXISTS_EXCEPTION.getCode(),CommonCodeEnum.PHONE_EXISTS_EXCEPTION.getMsg());
        }catch (UsernameExistException e) {
            e.printStackTrace();
            return R.error(CommonCodeEnum.USER_EXISTS_EXCEPTION.getCode(),CommonCodeEnum.USER_EXISTS_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    @RequestMapping("/login")
    public R login(@RequestBody MemberLoginVo vo){

        MemberEntity memberEntity =  memberService.login(vo);

        if(memberEntity != null){
            R r = R.ok();
            r.setData(memberEntity);
            return r ;
        }else {
            return R.error(CommonCodeEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getCode() , CommonCodeEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getMsg());
        }
    }

    @RequestMapping("/oauth/login")
    public R oauthLogin(@RequestBody WeiBoAuth weiBoAuth){

        MemberEntity memberEntity =  memberService.login(weiBoAuth);

        if(memberEntity != null){
            R r = R.ok();
            r.setData(memberEntity);
            return r;
        }else {
            return R.error(CommonCodeEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getCode() , CommonCodeEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getMsg());
        }
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }



}
